"""
Case Type   : 资源池化
Case Name   : 主备节点开启实时构建,缩容备机
Create At   : 2024.10
Owner       : chen
Description :
    1.开启实时构建
    ss_enable_ondemand_recovery=on
    ss_enable_ondemand_realtime_build=on
    recovery_parse_workers = 4
    recovery_redo_workers = 4
    hot_standby = off
    2.重启集群
    cm_ctl stop;cm_ctl start
    3.执行缩容备二
    gs_dropnode -U USER -G GROUP -h hostlist
    4.扩容备二，恢复环境
Expect      :
    1.设置guc参数成功
    2.重启成功
    3.缩容成功
    4.扩容恢复环境成功
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro

class SharedStorage(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_comsh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.com = Common()
        self.pri_node = Node('PrimaryDbUser')
        self.pri_root_node = Node('PrimaryRoot')
        self.sta2_node = Node('Standby2DbUser')
        self.db_host = self.sta2_node.db_host
        self.s_user = self.pri_node.ssh_user
        self.realtime = 'ss_enable_ondemand_realtime_build'
        self.realtime_default = self.com.show_param(self.realtime)
        self.recovery = 'ss_enable_ondemand_recovery'
        self.recovery_default = self.com.show_param(self.recovery)
        self.parse = 'recovery_parse_workers'
        self.parse_default = self.com.show_param(self.parse)
        self.redo = 'recovery_redo_workers'
        self.redo_default = self.com.show_param(self.redo)
        self.hot = 'hot_standby'
        self.hot_default = self.com.show_param(self.hot)
        
    def test_shared_storage(self):
        text = f'''-----step1: 开启实时构建 expect: 成功-----'''
        self.log.info(text)
        set_param1 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.realtime} = on")
        self.log.info(set_param1)
        self.assertTrue(set_param1, '参数修改失败')
        set_param2 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.recovery} = on")
        self.log.info(set_param2)
        self.assertTrue(set_param2, '参数修改失败')
        set_param3 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.parse} = 4")
        self.log.info(set_param3)
        self.assertTrue(set_param3, '参数修改失败')
        set_param4 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.redo} = 4")
        self.log.info(set_param4)
        self.assertTrue(set_param4, '参数修改失败')
        set_param5 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.hot} = off")
        self.log.info(set_param5)
        self.assertTrue(set_param5, '参数修改失败')

        text = f'''-----step2: 重启集群 expect: 成功-----'''
        self.log.info(text)
        restart = self.pri_comsh.exec_cm_ctl('stop && cm_ctl start')
        self.log.info(restart)
        self.assertIn(self.constant.cm_start_success_msg, restart)

        text = f'''-----step3: 执行缩容备二 expect: 缩容成功-----'''
        self.log.info(text)
        cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 600
        spawn gs_dropnode -U {self.s_user} -G {self.s_user} \
            -h {self.db_host}
        expect "*drop the target node (yes/no)?*"
        send "yes\\n"
        expect eof\n''' + '''EOF'''
        self.log.info(cmd)
        res = self.pri_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Success to drop the target nodes', res)
        cmd = f'source {macro.DB_ENV_PATH}; gs_uninstall --delete-data'
        self.log.info(cmd)
        res = self.sta2_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn('Uninstallation succeeded', res)

    def tearDown(self):
        text = f'''-----step4: 恢复环境-----'''
        self.log.info(text)
        self.log.info('-----扩容备二-----')
        xml_path = macro.DB_XML_PATH
        cmd = f'''gs_expansion -U {self.s_user} -G \
            {self.s_user} -X {xml_path} -h {self.db_host}'''
        execute_cmd = f'''source {macro.DB_ENV_PATH};
        expect <<EOF
        set timeout 300
        spawn {cmd}
        expect "*assword*:"
        send "{self.pri_root_node.ssh_password}\\n"
        expect "*assword*:"
        send "{self.pri_root_node.ssh_password}\\n"
        expect "*lease repeat for database*:"
        send "{macro.GAUSSDB_INIT_USER_PASSWD}\\n"
        expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd)
        res = self.pri_root_node.sh(execute_cmd).result()
        self.log.info(res)
        self.assertIn(f'{self.db_host}:\tSuccess', res)
        self.log.info('-----恢复guc参数-----')
        set_param1 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.realtime} = {self.realtime_default}")
        self.log.info(set_param1)
        self.assertTrue(set_param1, '参数修改失败')
        set_param2 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.recovery} = {self.recovery_default}")
        self.log.info(set_param2)
        self.assertTrue(set_param2, '参数修改失败')
        set_param3 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.parse} = {self.parse_default}")
        self.log.info(set_param3)
        self.assertTrue(set_param3, '参数修改失败')
        set_param4 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.redo} = {self.redo_default}")
        self.log.info(set_param4)
        self.assertTrue(set_param4, '参数修改失败')
        set_param5 = self.pri_comsh.execute_gsguc('set',
                                             self.constant.GSGUC_SUCCESS_MSG,
                                             f"{self.hot} = {self.hot_default}")
        self.log.info(set_param5)
        self.assertTrue(set_param5, '参数修改失败')
        self.log.info('-----重启集群-----')
        restart = self.pri_comsh.exec_cm_ctl('stop && cm_ctl start')
        self.log.info(restart)
        self.assertIn(self.constant.cm_start_success_msg, restart)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')